export default {
  data() {
    return {
      rules: {
        name: [{ required: true, whitespace: true, message: '不能为空' }],
        ename: [{ required: true, whitespace: true, message: '不能为空' }],
        route: [{ validator: this.validatorRoute }],
        pid: [{ validator: this.validatorPid }],
        icon: [{ validator: this.validatorIcon }]
      }
    }
  },
  methods: {
    validatorIcon(rule, value, callback) {
      if (value) {
        if (this.icons.some(item => item.value === value)) {
          callback()
        } else {
          callback(new Error('图标名称只能在下拉中选中'))
        }
      } else {
        callback()
      }
    },
    validatorRoute(rule, value, callback) {
      if (value) {
        if (value.startsWith('/')) {
          callback()
        } else {
          callback(new Error('路由填写不合法'))
        }
      } else {
        callback()
      }
    },
    validatorPid(rule, value, callback) {
      // if (value === '') {
      if (typeof value == 'string') {
        callback(new Error('请选择一个顶层节点'))
      } else {
        callback()
      }
    }
  }
}
